<?php
// +----------------------------------------------------------------------
// | Description: 系统用户
// +----------------------------------------------------------------------
// | Author: linchuangbin <linchuangbin@honraytech.com>
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\admin\logic\MemberLogic;
use app\common\controller\MemberAuthController;
use app\common\token\token;
use app\common\validate\MemberValidate;
use app\common\event\AdminLogin;


class Member extends MemberAuthController
{

    public function detail()
    {
        $MemberLogic = MemberLogic::instance();
        $user = Token::instance()->getUserInfo();
        $uid = $user[$user->getPk()];
        $data = $MemberLogic->getMore($uid);
        $data['userInfo']->hidden(['groups']);
        event(AdminLogin::class, $uid);
        return $this->json_responce($data);
    }

    public function refresh()
    {
        $MemberLogic = MemberLogic::instance();
        $user = Token::instance()->getUserInfo();
        $uid = $user[$user->getPk()];
        $data['token'] = $MemberLogic->createJwt($user);
        $data['expire'] = $_SERVER['REQUEST_TIME'] + 60 * 60 * 24 * 30;
        event(AdminLogin::class, $uid);
        return $this->json_responce($data);
    }

    public function index()
    {
        $MemberLogic = MemberLogic::instance();
        $get = input_param([
            'page' => 1,
            'keywords' => '',
            'limit' => 20,
            'group_id' => '',
            'structure_id' => '',
            'post_id' => '',
        ]);
        MemberValidate::instance()->runCheck($get, '', __FUNCTION__);
        $data['list'] = $MemberLogic->getList($get);
        $data['count'] = $MemberLogic->getCount($get);
        return $this->json_responce($data);
    }

    public function read()
    {
        $MemberLogic = MemberLogic::instance();
        $get = input_param([
            'id' => '',
        ]);
        MemberValidate::instance()->runCheck($get, '', __FUNCTION__);
        $data = $MemberLogic->read($get['id']);
        return $this->json_responce($data);
    }

    public function save()
    {
        $MemberLogic = MemberLogic::instance();
        $param = $this->param;
        MemberValidate::instance()->runCheck($param, '', __FUNCTION__);
        $data = $MemberLogic->createData($param);
        return $this->json_responce();
    }

    public function update()
    {
        $MemberLogic = MemberLogic::instance();
        $param = $this->param;
        MemberValidate::instance()->runCheck($param, '', __FUNCTION__);
        $data = $MemberLogic->updateByID($param);
        return $this->json_responce();
    }

    public function delete()
    {
        $MemberLogic = MemberLogic::instance();
        $param = $this->param;
        MemberValidate::instance()->runCheck($param, '', __FUNCTION__);
        $data = $MemberLogic->delByID($param);
        return $this->json_responce();
    }

    public function deletes()
    {
        $MemberLogic = MemberLogic::instance();
        $param = $this->param;
        MemberValidate::instance()->runCheck($param, '', __FUNCTION__);
        $data = $MemberLogic->delByIDs($param);
        return $this->json_responce();
    }

    public function enables()
    {
        $MemberLogic = MemberLogic::instance();
        $param = $this->param;
        MemberValidate::instance()->runCheck($param, '', __FUNCTION__);
        $data = $MemberLogic->enableByIDs($param);
        return $this->json_responce();
    }

}
